import Vue from "vue";
import VueRouter from "vue-router";
import Login from "@/views/Login";
import Home from "@/views/Home";
import Users from "@/views/Users/list";
import Rights from "@/views/Rights";
import Roles from "@/views/Rights/roles";
import Orders from "@/views/Orders";
import { GET_TOKEN } from "@/util/storage";

Vue.use(VueRouter);

const routes = [
  {
    path: "/",
    redirect: "/home"
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/home",
    name: "Home",
    component: Home,
    children: [
      {
        path: "/users",
        name: "Users",
        component: Users
      },
      {
        path: "/rights",
        component: Rights
      },
      {
        path: "/roles",
        component: Roles
      },
      {
        path: "/orders",
        component: Orders
      }
    ]
  }
];
const router = new VueRouter({
  mode: "history",
  base: process.env.BASE_URL,
  routes
});

router.beforeEach((to, from, next) => {
  if (to.path === "/login") return next();
  const token = GET_TOKEN();
  console.log("token" + token);
  if (token == null) {
    next("/login");
  } else {
    next();
  }
});

export default router;
